<template>
  <div class="business-satisfaction-admin-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="12">
        <!-- <el-button :icon="Plus" type="primary" @click="handleEdit">
          添加
        </el-button> -->
        <el-form :inline="true" :model="queryForm" @submit.prevent>
          <el-space>
            <el-form-item>
              <el-cascader
                v-model="queryForm.searchCompany"
                :options="CompanyOptions"
                :props="companyProps"
                clearable
                collapse-tags
                placeholder="选择组织"
                style="width: 200px"
                @change="getDeptment"
              />
            </el-form-item>
            <el-form-item>
              <el-cascader
                v-model="queryForm.searchDeptment"
                :options="DeptmentOptions"
                :props="deptmentProps"
                clearable
                collapse-tags
                placeholder="选择部门"
                style="width: 200px"
              />
            </el-form-item>
            <el-form-item class="form-item">
              <el-cascader
                v-model="queryForm.searchProductClass"
                :options="ProductClassOptions"
                :props="ProductClassSelect"
                placeholder="选择业务类型"
                clearable
                style="width: 160px"
              />
            </el-form-item>
            <!-- <el-form-item>
              <el-select v-model="queryForm.isKefu" placeholder="是否有客满" style="width:120px;">
                <el-option label="全部" :value="-1" />
                <el-option label="有客满" :value="0" />
                <el-option label="无客满" :value="0" />
              </el-select>
            </el-form-item> -->
            <el-form-item>
              <el-input
                v-model.trim="queryForm.searchKey"
                clearable
                placeholder="搜客户"
                style="width: 150px"
              />
            </el-form-item>
            <el-form-item>
              <el-input
                v-model.trim="queryForm.kefuStaff"
                clearable
                placeholder="搜客满"
                style="width: 120px"
              />
            </el-form-item>
            <el-form-item>
              <el-date-picker
                v-model="queryForm.PaymentDate"
                style="width: 240px"
                type="daterange"
                placeholder="按到账周期"
                start-placeholder="到账起始日期"
                end-placeholder="到账起始日期"
                value-format="YYYY-MM-DD"
              />
            </el-form-item>
            <el-form-item>
              <el-date-picker
                v-model="queryForm.ServiceStartDate"
                style="width: 240px"
                type="daterange"
                placeholder="按服务周期"
                start-placeholder="服务起始日期"
                end-placeholder="服务起始日期"
                value-format="YYYY-MM-DD"
              />
            </el-form-item>
            <el-form-item>
              <el-button :icon="Search" type="primary" @click="queryData">
                查询
              </el-button>
            </el-form-item>
            <el-form-item>
              <el-button type="danger" @click="handleDelete">
                批量分配
              </el-button>
            </el-form-item>
          </el-space>
        </el-form>
      </vab-query-form-left-panel>
      <!-- <vab-query-form-right-panel :span="12">
        <el-form :inline="true" :model="queryForm" @submit.prevent>
          <el-form-item>
            <el-input
              v-model.trim="queryForm.title"
              clearable
              placeholder="请输入标题"
            />
          </el-form-item>
          <el-form-item>
            <el-button :icon="Search" type="primary" @click="queryData">
              查询
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-right-panel> -->
    </vab-query-form>
    <el-tabs
      v-model="queryForm.tab"
      type="border-card"
      @tab-click="handleTabClick"
    >
      <!-- <el-tab-pane label="全部" name="all">
        <el-table
          v-loading="listLoading"
          :data="list"
          @selection-change="setSelectRows"
        >
          <el-table-column
            align="center"
            show-overflow-tooltip
            type="selection"
          />
          <el-table-column
            align="center"
            label="业务ID"
            prop="OrderID"
            show-overflow-tooltip
            width="80"
          />
          <el-table-column
            align="center"
            label="客户ID"
            prop="CustomerID"
            show-overflow-tooltip
            width="80"
          />
          <el-table-column
            align="left"
            label="客户名称"
            prop="CustomerAllName"
            show-overflow-tooltip
          />
          <el-table-column
            align="left"
            label="合同编号"
            prop="OrderCode"
            show-overflow-tooltip
          />
          <el-table-column
            align="left"
            label="业务类别"
            prop="BusinessOrderName"
            show-overflow-tooltip
          />
          <el-table-column align="center" label="签单人" width="200">
            <template #default="{ row }">
              <span>{{ row.SaleStaffName }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" label="店长组" width="200">
            <template #default="{ row }">
              <div v-if="row.StoreManager.length">
                <div v-for="(itm, idx) in row.StoreManager" :key="idx">{{ itm }}</div>
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center" label="客满专员" width="200">
            <template #default="{ row }">
              <div v-if="row.KefuStaffName.length">
                <div v-for="(itm, idx) in row.KefuStaffName" :key="idx">{{ itm }}</div>
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center" label="操作" width="200">
            <template #default="{ row }">
              <el-button type="primary" @click="handleEdit(row)">
                分配客满专员
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          background
          :current-page="queryForm.pageNo"
          :layout="layout"
          :page-size="queryForm.pageSize"
          :total="total"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        />
      </el-tab-pane> -->
      <el-tab-pane label="待分配" name="waitAssign">
        <el-table
          v-loading="listLoading"
          :data="list"
          @selection-change="setSelectRows"
        >
          <el-table-column
            align="center"
            show-overflow-tooltip
            type="selection"
            width="50"
          />
          <el-table-column
            align="center"
            label="业务ID"
            prop="OrderID"
            show-overflow-tooltip
            width="80"
          />
          <el-table-column
            align="center"
            label="客户ID"
            prop="CustomerID"
            show-overflow-tooltip
            width="80"
          />
          <el-table-column
            align="left"
            label="客户名称"
            prop="CustomerAllName"
            show-overflow-tooltip
          />
          <el-table-column
            align="left"
            label="合同编号"
            prop="OrderCode"
            show-overflow-tooltip
          />
          <el-table-column
            align="left"
            label="业务类别"
            prop="BusinessOrderName"
            show-overflow-tooltip
          />
          <el-table-column
            align="left"
            label="签约产品"
            prop="ProductName"
            show-overflow-tooltip
          />
          <el-table-column align="center" label="签单人" width="100">
            <template #default="{ row }">
              <span>{{ row.SaleStaffName }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" label="店长组" width="100">
            <template #default="{ row }">
              <div v-if="row.StoreManager.length">
                <div v-for="(itm, idx) in row.StoreManager" :key="idx">
                  {{ itm }}
                </div>
              </div>
              <div v-else>未分配</div>
            </template>
          </el-table-column>
          <el-table-column align="center" label="到账日期" width="120">
            <template #default="{ row }">
              <div v-for="(itm, idx) in row.PaymentDates" :key="idx">
                {{ itm }}
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center" label="服务周期" width="200">
            <template #default="{ row }">
              <span>{{ row.ServiceStartDate }}~{{ row.ServiceEndDate }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" label="查看详情" width="100">
            <template #default="{ row }">
              <el-button type="primary" @click="revieworder(row)">
                查看详情
              </el-button>
            </template>
          </el-table-column>
          <el-table-column align="center" label="客满专员" width="100">
            <template #default="{ row }">
              <div v-if="row.KefuStaffName.length">
                <div v-for="(itm, idx) in row.KefuStaffName" :key="idx">
                  {{ itm }}
                </div>
              </div>
              <div v-else>未分配</div>
            </template>
          </el-table-column>
          <!-- <el-table-column align="center" label="首次签单部门" width="200">
            <template #default="{ row }">
              <span>{{ row.SignDeptment }}</span>
            </template>
          </el-table-column> -->
          <el-table-column align="center" label="操作" width="150">
            <template #default="{ row }">
              <el-button type="primary" @click="handleEdit(row)">
                分配客满专员
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          background
          :current-page="queryForm.pageNo"
          :layout="layout"
          :page-size="queryForm.pageSize"
          :total="total"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        />
      </el-tab-pane>
      <el-tab-pane label="已分配" name="Assigned">
        <el-table
          v-loading="listLoading"
          :data="list"
          @selection-change="setSelectRows"
        >
          <el-table-column
            align="center"
            show-overflow-tooltip
            type="selection"
            width="50"
          />
          <el-table-column
            align="center"
            label="业务ID"
            prop="OrderID"
            show-overflow-tooltip
            width="80"
          />
          <el-table-column
            align="center"
            label="客户ID"
            prop="CustomerID"
            show-overflow-tooltip
            width="80"
          />
          <el-table-column
            align="left"
            label="客户名称"
            prop="CustomerAllName"
            show-overflow-tooltip
          />
          <el-table-column
            align="left"
            label="合同编号"
            prop="OrderCode"
            show-overflow-tooltip
          />
          <el-table-column
            align="left"
            label="业务类别"
            prop="BusinessOrderName"
            show-overflow-tooltip
          />
          <el-table-column
            align="left"
            label="签约产品"
            prop="ProductName"
            show-overflow-tooltip
          />
          <el-table-column align="center" label="签单人" width="100">
            <template #default="{ row }">
              <span>{{ row.SaleStaffName }}</span>
            </template>
          </el-table-column>
          <!-- <el-table-column align="center" label="首次签单部门" width="200">
            <template #default="{ row }">
              <span>{{ row.SignDeptment }}</span>
            </template>
          </el-table-column> -->
          <el-table-column align="center" label="店长组" width="100">
            <template #default="{ row }">
              <div v-if="row.StoreManager.length">
                <div v-for="(itm, idx) in row.StoreManager" :key="idx">
                  {{ itm }}
                </div>
              </div>
              <div v-else>未分配</div>
            </template>
          </el-table-column>
          <el-table-column align="center" label="到账日期" width="120">
            <template #default="{ row }">
              <div v-for="(itm, idx) in row.PaymentDates" :key="idx">
                {{ itm }}
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center" label="服务周期" width="200">
            <template #default="{ row }">
              <span>{{ row.ServiceStartDate }}~{{ row.ServiceEndDate }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" label="查看详情" width="100">
            <template #default="{ row }">
              <el-button type="primary" @click="revieworder(row)">
                查看详情
              </el-button>
            </template>
          </el-table-column>
          <el-table-column align="center" label="客满专员" width="100">
            <template #default="{ row }">
              <div v-if="row.KefuStaffName.length">
                <div v-for="(itm, idx) in row.KefuStaffName" :key="idx">
                  {{ itm }}
                </div>
              </div>
              <div v-else>未分配</div>
            </template>
          </el-table-column>
          <el-table-column align="center" label="操作" width="150">
            <template #default="{ row }">
              <el-button type="primary" @click="handleEdit(row)">
                调换客满专员
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          background
          :current-page="queryForm.pageNo"
          :layout="layout"
          :page-size="queryForm.pageSize"
          :total="total"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        />
      </el-tab-pane>
    </el-tabs>
    <edit ref="editRef" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import {
    // getOpenAreaCity,
    BusinessClassSelect,
    getCompanySelect,
    OperateDeptmentSelect,
  } from '@/api/common'
  import { getKefuAdminList } from '@/api/business'
  import Edit from './components/BusinessSatisfactionAdminEdit'
  import { Delete, Plus, Search } from '@element-plus/icons-vue'

  export default defineComponent({
    name: 'SatisfactionAdmin',
    components: { Edit },
    setup() {
      const router = useRouter()
      const $baseConfirm = inject('$baseConfirm')
      const $baseMessage = inject('$baseMessage')

      const state = reactive({
        editRef: null,
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        ProductClassOptions: [],
        DeptmentOptions: [],
        CompanyOptions: [],
        companyProps: {
          multiple: true,
          // checkStrictly: true,
          expandTrigger: 'hover',
        },
        deptmentProps: {
          multiple: true,
          // checkStrictly: true,
          expandTrigger: 'hover',
        },
        ProductClassSelect: {
          expandTrigger: 'hover',
        },
        queryForm: {
          pageNo: 1,
          pageSize: 10,
          tab: 'waitAssign',
        },
      })

      const setSelectRows = (val) => {
        state.selectRows = val
      }
      const handleEdit = (row) => {
        state.editRef.showEdit(row.WorkTaskID)
      }
      const handleDelete = (row) => {
        if (row.WorkTaskID) {
          $baseConfirm('你确定要删除当前项吗', null, async () => {
            // const { msg } = await doDelete({ ids: row.WorkTaskID })
            // $baseMessage(msg, 'success', 'vab-hey-message-success')
            // await fetchData()
            state.editRef.showEdit(row.WorkTaskID)
          })
        } else {
          if (state.selectRows.length > 0) {
            if (state.queryForm.tab == 'waitAssign') {
              state.tishi = '你确定要批量分配吗？'
            } else {
              state.tishi =
                '您正在已分配上操作批量分配，这样将替换原来的客满专员，确定要批量替换吗？'
            }
            const ids = state.selectRows.map((item) => item.WorkTaskID).join()

            $baseConfirm(state.tishi, null, async () => {
              // const { msg } = await doDelete({ ids })
              // $baseMessage(msg, 'success', 'vab-hey-message-success')
              // await fetchData()
              state.editRef.showEdit(ids)
            })
          } else {
            $baseMessage('未选中任何行', 'error', 'vab-hey-message-error')
          }
        }
      }
      const handleTabClick = (tab) => {
        console.log(tab.props.name)
        state.queryForm.tab = tab.props.name
        fetchData()
      }
      const handleSizeChange = (val) => {
        state.queryForm.pageSize = val
        fetchData()
      }
      const handleCurrentChange = (val) => {
        state.queryForm.pageNo = val
        fetchData()
      }
      const queryData = () => {
        state.queryForm.pageNo = 1
        fetchData()
      }
      const getDeptment = async () => {
        console.log('form.Company', state.queryForm.searchCompany)
        if (state.queryForm.searchCompany) {
          const getParams = {
            CompanyID: state.queryForm.searchCompany,
          }
          const {
            data: { DeptmentOptions },
          } = await OperateDeptmentSelect(getParams)
          state.DeptmentOptions = DeptmentOptions
        } else {
          state.queryForm.searchDeptment = []
          state.DeptmentOptions = []
        }
      }
      const fetchData = async () => {
        state.listLoading = true
        const {
          data: { CompanyOptions },
        } = await getCompanySelect()
        state.CompanyOptions = CompanyOptions
        const {
          data: { BusinessClassOptions },
        } = await BusinessClassSelect()
        state.ProductClassOptions = BusinessClassOptions
        const {
          data: { list, total },
        } = await getKefuAdminList(state.queryForm)
        state.list = list
        state.total = total
        state.listLoading = false
      }

      const revieworder = (row) => {
        const userInfo = JSON.parse(localStorage.getItem('userInfo'))
        const actstaffpost = JSON.parse(userInfo.StaffPostID)
        const actstaffpostid = actstaffpost[0][1]
        if (actstaffpostid === 11) {
          router.push({
            path: '/business/shopOperate/artAssignOrderDetail',
            query: { actstaffpostid: actstaffpostid, orderid: row.OrderID },
          })
        } else if (actstaffpostid === 43) {
          router.push({
            path: '/business/shopOperate/storeDetails',
            query: {
              actstaffpostid: actstaffpostid,
              OrderID: row.BusinessOrderID,
              WorkTaskID: row.WorkTaskID,
            },
          })
        } else {
          router.push({
            path: '/business/shopOperate/operateAssignOrder',
            query: { actstaffpostid: actstaffpostid, orderid: row.OrderID },
          })
        }
      }

      onMounted(() => {
        fetchData()
      })

      return {
        ...toRefs(state),
        setSelectRows,
        handleEdit,
        handleDelete,
        handleSizeChange,
        handleCurrentChange,
        queryData,
        handleTabClick,
        fetchData,
        getDeptment,
        revieworder,
        Delete,
        Plus,
        Search,
      }
    },
  })
</script>
